package com.gitee.qdbp.jdbc.plugins.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import com.gitee.qdbp.tools.utils.ReflectTools;

/**
 * 读取java.sql.Types中声明的静态字段值并缓存
 *
 * @author zhaohuihua
 * @version 20210429
 */
class JdbcDataTypeCache {

    public static Integer getJdbcType(String dataType) {
        return cache.get(dataType);
    }

    private static final Map<String, Integer> cache = new HashMap<>();
    static {
        Field[] fields = ReflectTools.getAllFields(Types.class, true, false, false);
        for (Field field : fields) {
            if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isStatic(field.getModifiers())) {
                continue;
            }
            if (field.getType() != int.class) {
                continue;
            }
            try {
                int value = field.getInt(null);
                cache.put(field.getName(), value);
            } catch (Exception e) {
                continue;
            }
        }
    }
}
